<?php
use I\App;
use I\DB;
use M\Middle_Public;

class Controller extends Middle_Public {
    public function index() {
        $this->adddata();
        return $this->view->html('stats-index');
    }

    public function adddata() {
        $this->view->addData([
            'pros' => DB::keyBy("select * from pros"),
            'tags' => DB::keyBy("select id, name, pro from tags order by id desc"),
        ]);
    }

    public function tag() {
        $pro = getgpc('pro');
        $tag = getgpc('tag');
        $this->view->addData([
            'pro_slt' => $pro,
            'tag_slt' => $tag,
        ]);

        $this->where("tag='$tag'");
        return $this->view->html('stats-index');
    }

    public function intime() {
        $t_start_show = getgpc('t_start');
        $t_end_show   = getgpc('t_end');

        $t_start = $t_start_show . ' 00:00:00';
        $t_end   = $t_end_show . ' 23:59:59';

        $this->view->addData([
            't_start_show' => $t_start_show,
            't_end_show'   => $t_end_show,
        ]);

        $this->where("updated_at>='$t_start' and updated_at<='$t_end'");

        return $this->view->html('stats-index');
    }

    private function where($where) {
        $this->adddata();

        $s_department   = array();
        $departments    = DB::keyBy("select * from titles where caty = " . App::Singleton()->getconfig('worktime', 'department'));
        $status         = App::Singleton()->getconfig('worktime', 'status');
        $default_status = array();
        foreach ($status as $status_id => $value) {
            $default_status[$status_id] = 0;
        }
        $default_status['new'] = 0;

        $db = DB::write();

        $a = $db->query("select count(*) as num, department as tname, status from tasks where $where group by department, status");

        $s_all = $default_status;
        foreach ($a as $row) {
            $s_all[$row->status] += $row->num;
        }

        $s_department = $this->getdata($a, $default_status);

        $a        = $db->query("select count(*) as num, leader as tname, status from tasks where $where group by leader, status order by department");
        $s_leader = $this->getdata($a, $default_status);

        $a     = $db->query("select count(*) as num, pro as tname, status from tasks where $where group by pro, status");
        $s_pro = $this->getdata($a, $default_status);

        $this->view->addData([
            'users'        => DB::keyBy("select id, name, department from users order by department"),
            'departments'  => $departments,
            's_all'        => $s_all,
            's_department' => $s_department,
            's_pro'        => $s_pro,
            's_leader'     => $s_leader,
        ]);

    }

    private function getdata($a, $default_status) {
        $rtn = array();
        foreach ($a as $row) {
            if (!isset($rtn[$row->tname])) {
                $rtn[$row->tname] = $default_status;
            }
            $rtn[$row->tname][$row->status] = $row->num;
        }

        return $rtn;
    }

    public function todaylast() {
        $this->adddata();
        $pro = getgpc('pro');
        $tag = getgpc('tag');

        if (!$tag) {
            return $this->view->html('stats-todaylast');
        }

        $today = getgpc('today');
        $t_end = strtotime($today . ' 23:59:59');

        $this->view->addData([
            'today'   => $today,
            'tag_slt' => $tag,
            'pro_slt' => $pro,
        ]);

        $this->where("deadline<='$t_end' and tag='$tag'");
        return $this->view->html('stats-todaylast');
    }

    public function preview() {
        $this->adddata();
        $pro        = getgpc('pro');
        $tag        = getgpc('tag');
        $only_dep   = getgpc('only_dep');
        $department = getgpc('department');
        $leader     = getgpc('leader', $this->authed->id);

        $today = getgpc('today');
        if ($today) {
            $t_end = strtotime($today . ' 23:59:59');
        } else {
            $t_end = time() + 86400 * 30;
        }

        $whereadd = '';
        if ($tag) {
            $whereadd .= " and tag = $tag";
        }

        if ($only_dep) {
            $whereadd .= " and department = $department";
        } else {
            $whereadd .= " and leader = $leader";
        }

        $db   = DB::write();
        $list = $db->query("select * from tasks where deadline<='$t_end' and status < 30 $whereadd order by leader, status, caty, priority desc, deadline");

        $this->view->addData([
            'today'       => $today,
            'tag_slt'     => $tag,
            'pro_slt'     => $pro,
            'department'  => $department,
            'leader'      => $leader,
            'only_dep'    => $only_dep,
            'list'        => $list,
            'users'       => DB::keyBy("select id, name, department from users order by department, team"),
            'departments' => DB::keyBy('select * from titles where caty = ' . App::singleton()->getconfig('worktime', 'department') . ' order by r '),
        ]);

        return $this->view->html('stats-preview');
    }
}
